package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.dangeroussourcemanage.api.dto.request.DangerSourceListRequest;
import com.smedi.ismedi.dangeroussourcemanage.api.dto.request.DangerSourceTimeLineRequest;
import com.smedi.ismedi.dangeroussourcemanage.api.dto.response.DangerSourceResponse;
import com.smedi.ismedi.dangeroussourcemanage.api.feign.RemoteDangerousSourceManageFeignApi;
import com.smedi.ismedi.inmail.core.application.query.vo.DangerSourceVo;
import com.smedi.ismedi.inmail.core.domain.dto.DangerSourceQuery;
import com.smedi.ismedi.inmail.core.domain.dto.TimeLineQuery;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.service.DangerSourceRemoteApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class DangerSourceRemoteApiImpl implements DangerSourceRemoteApi {
    private final RemoteDangerousSourceManageFeignApi remoteDangerousSourceManageFeignApi;
    private final AdapterConverter adapterConverter;

    @Override
    public List<DangerSourceVo> getDangerSourceList(DangerSourceQuery dangerSourceQuery) {
        DangerSourceListRequest dangerSourceListRequest = adapterConverter.dangerSourceQueryToDangerSourceListRequest(dangerSourceQuery);
        ResponseData<List<DangerSourceResponse>> responseData = remoteDangerousSourceManageFeignApi.getDangerSourceList(dangerSourceListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return adapterConverter.dangerSourceResponseToDangerSource(responseData.getData());
    }

    @Override
    public Integer getDangerSourceCount(DangerSourceQuery dangerSourceQuery) {
        DangerSourceListRequest dangerSourceListRequest = adapterConverter.dangerSourceQueryToDangerSourceListRequest(dangerSourceQuery);
        ResponseData<Integer> responseData = remoteDangerousSourceManageFeignApi.getDangerSourceCount(dangerSourceListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }

    @Override
    public Integer getMajorHazardCountByTimeLine(TimeLineQuery timeLineQuery) {
        DangerSourceTimeLineRequest dangerSourceTimeLineRequest = adapterConverter.timeLineQueryToDangerSourceTimeLineRequest(timeLineQuery);
        ResponseData<Integer> responseData = remoteDangerousSourceManageFeignApi.getDangerSourceCountByTimeLine(dangerSourceTimeLineRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }

}
